package org.example.sensor.repository;

import org.example.sensor.dox.SensorData;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface SensorDataRepository extends ListCrudRepository<SensorData, String> {
    @Query("SELECT * FROM sensor_data ORDER BY timestamp DESC LIMIT :n")
    List<SensorData> findTopNByOrderByTimestampDesc(@Param("n") int n);
    // 添加时间范围查询方法
    @Query("SELECT * FROM sensor_data WHERE timestamp BETWEEN :start AND :end ORDER BY timestamp DESC")
    List<SensorData> findByTimestampBetween(@Param("start") LocalDateTime start,
                                            @Param("end") LocalDateTime end);



    @Query("SELECT * FROM sensor_data WHERE timestamp BETWEEN :start AND :end ORDER BY timestamp DESC LIMIT :limit OFFSET :offset")
    List<SensorData> findPagedData(
            @Param("start") LocalDateTime start,
            @Param("end") LocalDateTime end,
            @Param("limit") int limit,
            @Param("offset") long offset);

    @Query("SELECT COUNT(*) FROM sensor_data WHERE timestamp BETWEEN :start AND :end")
    long countByTimestampBetween(
            @Param("start") LocalDateTime start,
            @Param("end") LocalDateTime end);
}
